package com.fcscs.cservice.client.main.configuration.reservation;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PaymentMethodDomainModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.domain.PaymentMethod;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class PayTypeGrid extends SimpleGridPanel<PaymentMethodDomainModelData> implements
		FormSubmitListener<PaymentMethod>,ModelSelectListener<PaymentMethodDomainModelData> {
	private static PayTypeGrid INSTANCE;
	private PropertyIdNameModelData currentProperty;
	private PayTypeDialog dialog;
	private PaymentMethodDomainModelData currentModel;
	
	public static PayTypeGrid getInstance(String title,
			ModelSelectListener<PaymentMethodDomainModelData> modelSelectListener){
    	if(null == INSTANCE){
    		INSTANCE = new PayTypeGrid(title, modelSelectListener);
    	}
    	return INSTANCE;
    }

	private PayTypeGrid(
			String title,
			ModelSelectListener<PaymentMethodDomainModelData> modelSelectListener) {
		super(title, null, (Object[]) null);
		setCollapsible(false);
		registerListeners(this);
		setSize(730, 520);
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<PaymentMethod> form,
			SubmitType submitType) {
		dialog.getFormPanel().populateEntity(currentModel.getEntity());
		savePayMethod(currentModel.getEntity());
	}
	
	@Override
	protected ColumnModel createColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		
		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CoreConstants.FULLTNAME,
				CoreConstants.CORE_CONSTANTS.name(),160);
		columns.add(cc);
		columns.add(CoreUtil.createNonSortableColumnConf
				(CServiceConstants.NAME_SEC_LANG,CoreConstants.CORE_CONSTANTS.secondaryName(), 160));
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,CoreConstants.CORE_CONSTANTS.action(), 160);
		cc.setRenderer(new GridCellRenderer<PaymentMethodDomainModelData>() {
			public Object render(final PaymentMethodDomainModelData model,String property, ColumnData config, int rowIndex,
					int colIndex, ListStore<PaymentMethodDomainModelData> store,final Grid<PaymentMethodDomainModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createEditBtn(model);
				HighLightIconButton id = createDeleteBtn(model, store);
				ib.setStyleAttribute("marginLeft", "3px");
				id.setStyleAttribute("marginLeft","3px");
				ib.setId("PayTypeGrid_edit_button");
				id.setId("PayTypeGrid_dele_button");
				hr.add(ib);
				hr.add(id);
				return hr;
			}			
		});
		columns.add(cc);
		ColumnModel columnModel = new ColumnModel(columns);
		return columnModel;
	}
	
	private HighLightIconButton createEditBtn(final PaymentMethodDomainModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						showEditPayTypeDialog(model);
					}
				});
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}
	
	private HighLightIconButton createDeleteBtn(final PaymentMethodDomainModelData model,
			final ListStore<PaymentMethodDomainModelData> store) {
		HighLightIconButton deleBtn = new HighLightIconButton(CoreConstants.CSS_DELETE_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				MessageBox.confirm(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.delete(), ShareGWTConstants.SHARE_GWT_CONSTANTS.paymentType()),
						ShareGWTConstants.SHARE_GWT_MESSAGES.whetherOperation(CoreConstants.CORE_CONSTANTS.delete().toLowerCase(), model.getEntity().getMethod()),
						new Listener<MessageBoxEvent>() {
					@Override
					public void handleEvent(MessageBoxEvent be) {
					if(Dialog.YES.equals(be.getButtonClicked().getItemId())){
						deletePayment(model, store);
					}
					}
				});
			}		
		});
		deleBtn.setId("PayTypeGrid_deletebtn_id");
		deleBtn.setTitle(CoreConstants.CORE_CONSTANTS.delete());
		return deleBtn;
	}
	
	private void deletePayment(final PaymentMethodDomainModelData model,
			final ListStore<PaymentMethodDomainModelData> store){
		CServiceCommonServiceController.getINSTANCE().deletePayMethod(
			model.getEntity(), new AsyncCallbackListener<Boolean>(){
				@Override
				public void onLogin(String username) {
					CServiceCommonServiceController.getINSTANCE().deletePayMethod(
							model.getEntity(),this);
				}
				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
							CoreConstants.CORE_CONSTANTS.systemError(), null);
				}
				@Override
				public void onSuccess(Boolean result) {
					if (null != result && result) {
						store.remove(model);
						CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.deleted());
					}
				}
			});
	}

	@Override
	public void onModelClick(PaymentMethodDomainModelData model) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onModelDoubleClick(PaymentMethodDomainModelData model) {
		showEditPayTypeDialog(model);		
	}

	@Override
	public void onModelEnterPress(PaymentMethodDomainModelData model) {
		showEditPayTypeDialog(model);		
	}

	private void showEditPayTypeDialog(PaymentMethodDomainModelData model){
		currentModel = model;
		dialog=PayTypeDialog.getInstance(this, false, currentProperty);
		dialog.getFormPanel().updateForm(model.getEntity());
		dialog.show();
	}

	public void setCurrentProperty(PropertyIdNameModelData currentProperty) {
		this.currentProperty = currentProperty;
	}
	
    private void savePayMethod(final PaymentMethod payMethod){
    	CServiceCommonServiceController.getINSTANCE().savePayMethod(
    		payMethod, new AsyncCallbackListener<PaymentMethod>(){
				@Override
				public void onLogin(String username) {
					CServiceCommonServiceController.getINSTANCE().savePayMethod(
				    	payMethod, this);
				}
				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
						CoreConstants.CORE_CONSTANTS.systemError(), null);
				}
				@Override
				public void onSuccess(PaymentMethod result) {
					if(null != result){
						PaymentMethodDomainModelData newModel = new PaymentMethodDomainModelData(result);
						replaceModel(currentModel, newModel);
						currentModel = newModel;
						
						dialog.hide();
						
						CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
					}
				}
    		});
	}
}
